/**
 * 
 */
export class CancelablePromise extends Promise{
  constructor(props) {
    super(props)
    this._hasCanceled = false

    const _s = this
    this.wrapper =  ( callback ) => {
      return (value) => {
        if( !_s._hasCanceled ) {
          if(callback)
            callback(value)
        } else {
        }
      }
    }
  }
  then(onFulfilled,onRejected){
    super.then(this.wrapper(onFulfilled),this.wrapper(onRejected))
    return this
  }

  catch(onRejected){
    super.catch(this.wrapper(onRejected))
    return this
  }
  resolve(value) {
    if(!this._hasCanceled){
      super.resolve(value)
    } else{
    }
  }
  reject(value){
    if(!this._hasCanceled){
      super.reject(value)
    } else {
    }
  }
  cancel() {
    this._hasCanceled = true
  }
}
CancelablePromise.all = (iterable) => {
  const promise = Promise.all(iterable)

  const _s = this
  return new CancelablePromise((resolve, reject) => {
    promise.then(data => {
      resolve(data)
    })
    promise.catch(e => {
      reject (e)
    })
  })
}
